mini.parse();

function CouponRuleFormHandler() {
};

CouponRuleFormHandler.instance = new CouponRuleFormHandler();

//获得全局的规则表单对象
CouponRuleFormHandler.couponRuleForm = new mini.Form("couponRuleForm");
CouponRuleFormHandler.formData = {};

// 规则启用你状态数据
CouponRuleFormHandler.bcrStateData = [
	{text: "启用",value: 1},
	{text: "不启用",value: 0}
];
mini.get("bcrState").setData(CouponRuleFormHandler.bcrStateData);

/**
 * 保存（添加或编辑）数据的方法
 */
CouponRuleFormHandler.save = function(){
	var couponRuleData = CouponRuleFormHandler.couponRuleForm.getData();
	couponRuleData.bcrId = CouponRuleFormHandler.formData.bcrId;
	
    CouponRuleFormHandler.couponRuleForm.validate();
    if (CouponRuleFormHandler.couponRuleForm.isValid() == false) return;
    
    var json = mini.encode(couponRuleData);
    $.ajax({
        url: __root + "/saveOrUpdateCouponRule.do",
        type: "POST",
        data: {
        	couponRuleInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        cache: false,
        success: function (text) {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		CouponRuleFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示",jqXHR.responseText, function(){
        		CouponRuleFormHandler.closeCurrentWin();
        	});
        }
    });
    
}

/**
 * 标准方法接口定义
 * @param {} data
 */
CouponRuleFormHandler.passData = function(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        CouponRuleFormHandler.formData.bcrId = data.bcrId;
        //根据id获取对应的规则信息
        $.ajax({ 
            url: __root + "/getCouponRuleById.do?bcrId=" + data.bcrId,
            cache: false,
            dataType: 'json',
            success: function (text) {
                CouponRuleFormHandler.couponRuleForm.setData(text);
            }
        });
    }else{
    	//默认类型状态为有效
    	mini.get("bcrState").setValue(1);
    }
}

/**
 * 关闭窗口
 * @param {} action
 */
CouponRuleFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
}

/**
 * 关闭窗口
 */
CouponRuleFormHandler.closeMiniWin = function(){
	CouponRuleFormHandler.closeCurrentWin("cancel");
}

/**
 * 规则表达式验证过程
 * @param {} e
 */
function validBcrRule(e){
	var bcrSql = mini.get("bcrSql");
	var expSql = "select (case when @@conditions end) into @exe_result";
	var expText = e.value.trim();
	
	if(/^$/.test(expText)){
		bcrSql.setValue("");
		e.errorText = "请填写规则表达式！";
        e.isValid = false;
	}else{
		$.ajax({
	        url: __root + "/validBgrRuleExp.do",
	        type: "POST",
	        data: {
	        	expText: expText
	        },
	        async: false,
	        success: function (text) {
	        	if(text == "1"){
	        		expSql = expSql.replace("@@conditions", expText);
	        		expSql = expSql.replace("==", " then ");
	        		bcrSql.setValue(expSql);
	        	}else{
	        		bcrSql.setValue("");
					e.errorText = "填写有效的表达式！";
			        e.isValid = false;
	        	}
	        }
	    });
	}
}

/**
 * 购物券规则填写说明
 */
function bcrRuleHelpInfo(){
	var title = "帮助提示：";
	var html = "<font color='red'>&nbsp;&nbsp;"
			 + "1、填写的表达式基本格式为﹛条件 == 值﹜。<br/>&nbsp;&nbsp;" 
			 + "2、表达式中允许出现的符号：</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "①、算数运算符 +、-、*、/、%</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "②、关系运算符 >、<、>=、<=、=、!=<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
			 + "③、逻辑运算符 ||、&&<br/>&nbsp;&nbsp;" 
			 + "3、我们已为您提供的系统变量：@goods_total (商品总额)。<br/>&nbsp;&nbsp;" 
			 + "4、举例：(@goods_total >= 20) && (@goods_total <= 50) == 10"
			 + "</font>";
	
	mini.showMessageBox({
		width: 420,
		height: 240,
        title: title,
        html: html,
        buttons: ["ok"],
        callback: null
    });
}